package cn.mily.datadir.controller;

import cn.mily.basics.utils.JsonResult;
import cn.mily.basics.utils.PageBean;
import cn.mily.datadir.domain.DataDetail;
import cn.mily.datadir.domain.DataDir;
import cn.mily.datadir.query.DataDetailQuery;
import cn.mily.datadir.service.IDataDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/detail")
public class DataDetailController {

    @Autowired
    private IDataDetailService service;



    @PatchMapping/*根据id数组删除批量*/
    public JsonResult patchDelete(@RequestBody Long[] ids){
        try {
            service.patchDelete(ids);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult("删除失败！");
        }
    }

    /*删除*/
    @DeleteMapping("/{id}")/*根据id删除一个*/
    public JsonResult deleteById(@PathVariable Long id){
        try {
            service.delete(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult("删除失败！");
        }
    }

    /*添加*/
    @PutMapping/*添加或修改*/
    public JsonResult add(@RequestBody DataDetail detail){
        //获取id
        if (detail.getId()==null){/*没有id为添加*/
            try {
                service.add(detail);
                return new JsonResult();
            } catch (Exception e) {
                e.printStackTrace();
                return new JsonResult("添加失败！");
            }
        }else {
            try {/*有id为修改*/
                service.update(detail);
                return new JsonResult();
            } catch (Exception e) {
                e.printStackTrace();
                return new JsonResult("修改失败！");
            }
        }

    }

    /*根据数据种类id查询所有*/
    @PostMapping
    public PageBean<DataDir> getDataDirs(@RequestBody DataDetailQuery query){
        System.out.println(query);
        PageBean dataDirs = service.findAll(query);
        return dataDirs;
    }
}
